import template from './regionCreate.view.html';
import viewController from './regionCreate.controller';

class Controller {
    constructor (systemService, $stateParams, $scope, $uiModal, uiNotification) {
        "ngInject"
        this.uiModal = $uiModal;
        this.notify  = uiNotification;
        this.service = systemService;
        this.data    = {parRegionId: 0, statusCd: 'S0A'};
        this.cache   = {};
        // 获取根树
        systemService.getRegionTree(this.data).then(res => {
            this.treeNode = res.data;
        })
    }

    toggleConfirm (node) {
        node.confirm = !node.confirm
    }

    toggleNode (scope, node, $event) {
        __DEBUG__ && console.log(node)
        __DEBUG__ && console.log(this.cache)
        $event.preventDefault();
        if (node.hasChildren) {
            if ( scope.collapsed ) {
                scope.expand();
                if (this.cache[node.id]) {
                    return node.nodes = this.cache[node.id]
                }
                node.loading          = true;
                this.data.parRegionId = node.id;
                this.service.getRegionTree(this.data).then((res)=> {
                    this.cache[ node.id ] = res.data;
                    node.nodes            = this.cache[ node.id ];
                }).catch((data) => {
                    this.notify.error({
                        'title'  : '提示',
                        'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
                    });
                }).finally(() => {
                    node.loading = false;
                });
            } else {
                scope.collapse();
            }
        }
    }

    removeNode (scope, node) {
        __DEBUG__ && console.log(this.cache)
        if (this.deleting) return;
        this.deleting = true;
        this.service.delItem('region', {commonRegionId: node.id}).then(() => {
            let idx = _.findIndex(this.cache[node.parRegionId], {id: node.id});
            this.cache[node.parRegionId].splice(idx, 1);
            if (!this.cache[node.parRegionId].length) {
                this.cache[node.parRegionId].hasChildren = false;
            }
            this.deleting = false;
            this.notify.success({
                'title'  : '提示',
                'message': '操作成功!'
            });
        }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        }).finally(() => {

        });
    }

    openView (scope, node, type, $e) {
        __DEBUG__ && console.log(scope)
        __DEBUG__ && console.log(node)
        __DEBUG__ && console.log(this.cache)
        let isEdit   = type == 'edit' ? true : false;

        if (node.hasChildren && $e) this.toggleNode(scope, node, $e)

        if (!this.cache[node.id]) {
            this.cache[node.id] = [];
            node.nodes = this.cache[node.id];
        }

        let modalInstance = this.uiModal.open({
            animation   : true,
            templateUrl : template,
            controller  : viewController,
            controllerAs: 'vm',
            size        : 400,
            resolve     : {
                item: () => {
                    return {
                        node: node,
                        type: type
                    }
                }
            }
        });

        // 处理对话框提交/关闭
        modalInstance.result.then((res) => {
            if ( isEdit ) {
                let item = _.find(this.cache[node.parRegionId],{id: node.id})
                item.regionName = res.regionName;
                item.regionDesc = res.regionDesc;
            } else {
                node.hasChildren = true;
                scope.expand();

                if (this.cache[node.id] && this.cache[node.id].nodes) {
                    this.cache[node.id].nodes.push(res)
                } else {
                    this.cache[node.id].push(res)
                }
            }
            __DEBUG__ && console.log(this.cache)
        });
    }
}
export default Controller;